package com.gzyj.web.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lhj
 */
@SpringBootConfiguration
public class RedisConfig {

    //redis地址
    @Value("${spring.redis.host}")
    private String host;


    @Value("${spring.redis.password}")
    private String pass;


    @Bean
    RedisTemplate<String,Object> clusterRedisTemplate(){
        RedisClusterConfiguration server = new RedisClusterConfiguration();
        List<RedisNode> nodes=new ArrayList<>();
        RedisNode redisNode7001=new RedisNode(host,7001);
        nodes.add(redisNode7001);
        RedisNode redisNode7002=new RedisNode(host,7002);
        nodes.add(redisNode7002);
        RedisNode redisNode7003=new RedisNode(host,7003);
        nodes.add(redisNode7003);

        RedisNode redisNode8001=new RedisNode(host,8001);
        nodes.add(redisNode8001);
        RedisNode redisNode8002=new RedisNode(host,8002);
        nodes.add(redisNode8002);
        RedisNode redisNode8003=new RedisNode(host,8003);
        nodes.add(redisNode8003);

        server.setClusterNodes(nodes);

        GenericObjectPoolConfig genericObjectPoolConfig =
                new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(4);
        genericObjectPoolConfig.setMinIdle(0);
        genericObjectPoolConfig.setMaxTotal(8);
        genericObjectPoolConfig.setMaxWaitMillis(10000);

        //redis客户端配置
        LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder
                builder =  LettucePoolingClientConfiguration.builder().
                commandTimeout(Duration.ofMillis(10*1000));

        builder.shutdownTimeout(Duration.ofMillis(4000));
        builder.poolConfig(genericObjectPoolConfig);
        LettuceClientConfiguration lettuceClientConfiguration = builder.build();


//        server.setPassword(pass);
        LettuceConnectionFactory factory = new LettuceConnectionFactory(server,lettuceClientConfiguration);
        factory.afterPropertiesSet(); //刷新配置

        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new FastJsonRedisSerializer<>(Object.class));
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new FastJsonRedisSerializer<>(Object.class));

        return redisTemplate;
    }


}
